package com.hzw.saas.service.hpc.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.hpc.IHpcAccountAmountDailyService;
import com.hzw.saas.api.hpc.bo.HpcAccountAmountDailyBo;
import com.hzw.saas.common.util.LocalDateTimeUtils;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.common.util.SnowFlakeUtil;
import com.hzw.saas.service.hpc.mapper.HpcAccountAmountDailyMapper;
import com.hzw.saas.service.hpc.model.HpcAccountAmountDaily;
import jodd.util.StringUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author sonam
 * @since 2021/5/31 17:02
 */
@Service("hpcAccountAmountDailyServiceImpl")
public class HpcAccountAmountDailyServiceImpl extends ServiceImpl<HpcAccountAmountDailyMapper, HpcAccountAmountDaily> implements IHpcAccountAmountDailyService, IService<HpcAccountAmountDaily> {

    @Override
    public HpcAccountAmountDailyBo save(HpcAccountAmountDailyBo hpcAccountAmountDailyBo) {
        if(hpcAccountAmountDailyBo == null) return null;
        HpcAccountAmountDaily hpcAccountAmountDaily = MapperUtil.nf().map(hpcAccountAmountDailyBo, HpcAccountAmountDaily.class);
        hpcAccountAmountDaily.setIsDelete(0);

        if(StringUtil.isBlank(hpcAccountAmountDaily.getPid())) hpcAccountAmountDaily.setPid(SnowFlakeUtil.nextIdStr());
        super.save(hpcAccountAmountDaily);
        return MapperUtil.nf().map(hpcAccountAmountDaily, HpcAccountAmountDailyBo.class);
    }

    @Override
    public HpcAccountAmountDailyBo update(HpcAccountAmountDailyBo hpcAccountAmountDailyBo) {
        if(hpcAccountAmountDailyBo == null) return null;
        if(StringUtil.isBlank(hpcAccountAmountDailyBo.getPid())) return null;

        HpcAccountAmountDaily hpcAccountAmountDaily = MapperUtil.nf().map(hpcAccountAmountDailyBo, HpcAccountAmountDaily.class);
        super.updateById(hpcAccountAmountDaily);

        return MapperUtil.nf().map(hpcAccountAmountDaily, HpcAccountAmountDailyBo.class);
    }

    @Override
    public HpcAccountAmountDailyBo getOne(HpcAccountAmountDailyBo hpcAccountAmountDailyBo) {
        if(hpcAccountAmountDailyBo == null) return null;
        HpcAccountAmountDaily hpcAccountAmountDaily = MapperUtil.nf().map(hpcAccountAmountDailyBo, HpcAccountAmountDaily.class);
        List<HpcAccountAmountDaily> list = this.lambdaQuery().setEntity(hpcAccountAmountDaily).list();
        if(CollectionUtils.isEmpty(list)) return null;
        return MapperUtil.nf().map(list.get(0), HpcAccountAmountDailyBo.class);
    }

    @Override
    public Map<String, BigDecimal> findHpcAccountAmountDailyBo(List<String> recordIds) {
        if(CollectionUtils.isEmpty(recordIds)) {
            return new HashMap<>();
        }
        List<HpcAccountAmountDaily> list = this.lambdaQuery().in(HpcAccountAmountDaily::getHpcAccountRecordId,recordIds).list();
        LocalDate now = LocalDate.now();
        HashMap<String, BigDecimal> stringBigDecimalHashMap = new HashMap<>();
        list.forEach(hpcAccountAmountDaily -> {
            LocalDate localDate = LocalDateTimeUtils.convertDateToLDT(hpcAccountAmountDaily.getBusTime()).toLocalDate();
            long l = LocalDateTimeUtils.betweenTwoTime(localDate, now, ChronoUnit.DAYS);
            if ( l == 1L){
                stringBigDecimalHashMap.put(hpcAccountAmountDaily.getHpcAccountRecordId(),new BigDecimal(hpcAccountAmountDaily.getCurCoreTimeBalance()));
            }
        });
        return stringBigDecimalHashMap;
    }

}
